<script setup>
import {Upload } from '@element-plus/icons-vue'
  import TextEitdor from '@/views/text/TextEitdor.vue';
  import { ref,onMounted } from 'vue';
  import { faburenwu } from '@/api/announcemt';
  const form = ref({
  publisher: '',
  publishUnit: 6,
  publishDate: '',
  approval:1,
  title: '',        
  effectiveDate: '', 
  main: '',
});
  const ruleForm=ref(null)
  import { useUserStore } from '@/stores/user';
  import { ElMessage } from 'element-plus';
  const userstore=useUserStore()
  const user=userstore.user
  const rules = {
    title: [
      { required: true, message: '请输入公告标题', trigger: 'blur' },
    ],
    introduction: [
      { required: true, message: '请输入公告简介', trigger: 'blur' },
      { max: 20, message: '公告简介不能超过20个字', trigger: 'blur' }
    ],
    effectiveDate: [
      { required: true, message: '请选择生效日期', trigger: 'blur' },
    ],
    main: [
      { required: true, message: '请输入公告内容', trigger: 'blur' },
    ],
  };
  onMounted(()=>{
    form.value.publisher=user.id
    if(user.departmentId)form.value.publishUnit=user.departmentId
    form.value.publishDate=new Date().toISOString().split('T')[0];
  })
  const fabu = async () => {
    if (form.value.main !== '<p><br></p>') {
      try {
        await ruleForm.value.validate();
        const res = await faburenwu(form.value);
        ElMessage.success("上传成功！");
        form.value={}
      } catch (error) {
        ElMessage.error("表单校验失败，请检查输入！");
      }
    } else {
      ElMessage.error("内容不能为空！");
    }
  };
</script>
<template>
  <el-card>
    <h1 style="font-size: 30px;">上传公告</h1>
    <el-form
    ref="ruleForm"
    style="max-width: 600px;margin:0 auto;"
    :model="form"
    :rules="rules"
    label-width="auto"
    class="demo-ruleForm"
    :size="formSize"
    status-icon
    >
      <el-form-item label="公告标题" prop="title">
        <el-input v-model="form.title" />
      </el-form-item>
      <el-form-item label="公告简介" prop="introduction">
        <el-input v-model="form.introduction" />
      </el-form-item>
      <el-form-item label="生效时间" prop="effectiveDate">
        <el-date-picker
          v-model="form.effectiveDate"
          type="date"
          aria-label="请选择日期"
          placeholder="请选择日期"
          style="width: 100%"
          value-format="YYYY-MM-DD"/>
      </el-form-item>
      <el-form-item label="内容" prop="main">
        <TextEitdor v-model="form.main"/>
      </el-form-item>
      <el-button @click="fabu"  type="success">上传公告<el-icon class="el-icon--right"><Upload /></el-icon></el-button>
    </el-form>
  </el-card>
</template>
<style scoped>
  
</style>